package com.suitsoft.bdms.web.account;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.suitsoft.bdms.entity.account.Role;
import com.suitsoft.bdms.entity.account.SubSystem;
import com.suitsoft.bdms.service.account.AccountManager;
import com.suitsoft.bdms.service.account.SystemManager;
import com.suitsoft.bdms.web.CrudActionSupport;

/**
 * 系统
 *@author Suineg
 */
@SuppressWarnings("serial")
public class SystemAction extends CrudActionSupport<SubSystem> {
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	//
	private SystemManager systemManager;
	private AccountManager accountManager;
	
	/** 系统实体 */
	private SubSystem system;
	/** ID */
	private Long id;
	/** 分页对象 */
	private Page<SubSystem> page = new Page<SubSystem>(10);
	/** 被选中的角色ID */
	private List<Long> checkdIds;
	/** 一个系统下的所有 角色 */
	private List<Role> roleList;

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		page = systemManager.searchSystem(page, filters);
		return SUCCESS;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			system = systemManager.get(id);
		} else {
			system = new com.suitsoft.bdms.entity.account.SubSystem();
		}
	}
	
	public String preInput(){
		system = systemManager.get(id);
		roleList = accountManager.findRoleBySystem(id);
		checkdIds = system.getRoleIds();
		return "role";
	}

	@Override
	public String save() throws Exception {
		if(id == null || id == 0l){
			addActionMessage("非法操作！");
			return input();
		}
		systemManager.save(system,checkdIds);
		return list();
	}

	@Override
	public SubSystem getModel() {
		// TODO Auto-generated method stub
		return system;
	}
	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}
	public SubSystem getSystem() {
		return system;
	}
	public void setSystem(SubSystem system) {
		this.system = system;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Page<SubSystem> getPage() {
		return page;
	}
	public void setPage(Page<SubSystem> page) {
		this.page = page;
	}

	public List<Long> getCheckdIds() {
		return checkdIds;
	}

	public void setCheckdIds(List<Long> checkdIds) {
		this.checkdIds = checkdIds;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
	
}

